/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   14-12-26 下午5:32 Created by dwzhang
 */
package com.jade.modules.software.impl;

import java.util.Date;
import java.util.List;
import javax.ejb.TransactionAttribute;
import javax.inject.Inject;
import javax.inject.Named;

import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Order;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.framework.base.vfile.VFileConstants;
import com.jade.framework.base.vfile.VFileUtils;
import com.jade.modules.software.Software;
import com.jade.modules.software.SoftwareDao;
import com.jade.modules.software.SoftwareException;
import com.jade.modules.software.SoftwareManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 软件包管理器。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 14-12-26 下午5:32
 */
//@Named ("module_softwareManager")
public class SoftwareManagerImpl
        implements SoftwareManager
{
    protected SoftwareDao softwareDao;

//    @Inject
    public void setSoftwareDao(@Named ("module_softwareDao") SoftwareDao softwareDao)
    {
        this.softwareDao = softwareDao;
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void addSoftware(Software software)
            throws SoftwareException
    {
        software.setCreationDate(new Date());
        softwareDao.addSoftware(software);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void updateSoftware(Software software)
            throws SoftwareException
    {
        Software old = getSoftware(software.getId());
        if (software.getFilePath() == null) {
            software.setFilePath(old.getFilePath());
            software.setFileName(old.getFileName());
        }
        else if (StringUtils.isNotBlank(old.getFilePath()) &&
                StringUtils.equalsIgnoreCase(software.getFilePath(), old.getFilePath())) {
            //remove old file
            VFileUtils.removeFileQuitely(VFileConstants.DATA_TYPE_SOFTWARE, old.getFilePath());
            software.setFilePath(old.getFilePath());
            software.setFileName(old.getFileName());
        }
        softwareDao.updateSoftware(software);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void removeSoftware(long... ids)
            throws SoftwareException
    {
        for (long id : ids) {
            Software software = getSoftware(id);
            if (software != null) {
                VFileUtils.removeFileQuitely(VFileConstants.DATA_TYPE_SOFTWARE, software.getFilePath());
                softwareDao.removeSoftware(software);
            }
        }
    }

    public Software getSoftware(long id)
    {
        return softwareDao.getSoftware(id);
    }

    public Software getNewestSoftware(String type)
    {
        Criteria criteria = new Criteria(0, 1);
        criteria.add(Restrictions.eq("type", type));
        criteria.addOrder(Order.desc("version"));
        List<Software> softwares = listSoftware(criteria);
        return softwares.isEmpty() ? null : softwares.get(0);
    }

    public List<Software> listSoftware(Criteria criteria)
    {
        return softwareDao.listSoftware(criteria);
    }

    public PagingList<Software> querySoftware(Criteria criteria)
    {
        return softwareDao.querySoftware(criteria);
    }
}
